﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class CongnoKhAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
                LoadKhachhang();
            }
        }
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNO_KHRepository repository = new CONGNO_KHRepository(new EFRepository<CONGNO_KH>(), iunitfwork);
            CONGNO_KH objcongnokh = null;
            string strid = "" + Request["ID"];
            int id = 0;
            int.TryParse(strid, out id);
            if (id>0)
            {
                objcongnokh = repository.GetById(id);
                Dlkhachhang.SelectedValue = objcongnokh.KHACHHANG.MAKH;
                txtsotieno.Value = objcongnokh.SOTIENNO.ToString();
                txtghichu.Value = objcongnokh.GHICHU;
                if (objcongnokh.TINHTRANG == true)
                {
                    rddtt.Checked = true;
                }
                else
                {
                    rdchuatt.Checked = true;
                }

            }
            string strmakh = "" + Request["MAKH"];
            if (!string.IsNullOrEmpty(strmakh))
            {
                Dlkhachhang.SelectedValue = strmakh;
                txtsotieno.Value = Session["Tongtienkh"].ToString();
            }
        }
        public void LoadKhachhang()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            KHACHHANGRepository repository = new KHACHHANGRepository(new EFRepository<KHACHHANG>(), iunitfwork);
            IQueryable<KHACHHANG> objKhachhang = repository.All();
            Dlkhachhang.DataSource=objKhachhang;
            Dlkhachhang.DataValueField="MAKH";
            Dlkhachhang.DataTextField="TENKH";
            Dlkhachhang.DataBind();
            Dlkhachhang.Items.Insert(0, new ListItem { Text = "---Chọn khách hàng---", Value = "0" });
        }
        void capnhap()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNO_KHRepository repository = new CONGNO_KHRepository(new EFRepository<CONGNO_KH>(), iunitfwork);
            string strid = "" + Request["ID"];
            int id = 0;
            int.TryParse(strid, out id);
            CONGNO_KH objcongno = null;
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                iscapnhap = false;
                objcongno = repository.GetById(id);
            }
            else
            {
                objcongno = new CONGNO_KH();
                iscapnhap = true;
            }
            if (Dlkhachhang.SelectedValue.Equals("0"))
            { 
               RegisterClientScriptBlock("Thông báo","<script> alert ('Vui lòng chọn khách hàng')</script>");
                return;
            }
            objcongno.MAKH = Dlkhachhang.SelectedValue;
            objcongno.SOTIENNO= int.Parse(txtsotieno.Value);
            objcongno.GHICHU = txtghichu.Value;
            if (rddtt.Checked)
            {
                objcongno.TINHTRANG = true;
            }
            else
            {
                objcongno.TINHTRANG = false;
            }
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objcongno);
                }
                repository.Save();
            }
            catch (Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Đã tồn tại')</script>");
            }
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }

        protected void lbtnthemoi_Click(object sender, EventArgs e)
        {
            txtghichu.Value = "";
            txtsotieno.Value = "";
            Dlkhachhang.SelectedValue = "0";
            Response.Redirect("CongnoKhAdd.aspx");
        }
    }
}